<template>
  <div class="RetrievePassword">
    <Navbar>
      <template #left>
        <div class="close" @click="closeRetrievePasswordModal">
          <Icon name="zuojiantou" color="#ccc" size="18" />
        </div>
      </template>
    </Navbar>
    <form class="login-form">
      <h2>找回密码</h2>
      <p class="tips">请输入<span>{{ props.email }}</span>收到的6位验证码</p>
      <div class="login-form-item">
        <input type="text" autocomplete="email" v-model="code" placeholder="请输入邮箱验证码" />
        <!-- 获取验证码按钮：根据倒计时状态显示不同内容 -->
        <p class="get-code" :class="{ disabled: isCounting }" @click="handleGetCodeClick">
          {{ isCounting ? `重新获取 (${countdown}s)` : '获取验证码' }}
        </p>
      </div>
      <div class="login-form-item">
        <input type="password" autocomplete="new-password" v-model="newPassword" placeholder="请输入新密码" />
      </div>
    </form>
    <div class="correct" @click="resetPassword">
      <Icon name="dui" color="#ac5cc5" size="18" />
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, onUnmounted } from 'vue'
import Navbar from '@/components/common/Navbar.vue'
import Icon from '@/components/common/Icon.vue'
import { Toast } from '@/utils/showtoast'


const model = defineModel({ default: false })
const closeRetrievePasswordModal = () => (model.value = false)

const props = defineProps({
  email: {
    type: String,
    default: ''
  }
})

import { userGetCodeAPI, userResetPasswordAPI } from '@/api/login'

// 倒计时相关
const countdown = ref(30) // 初始30秒
const isCounting = ref(true) // 是否正在倒计时
let timer = null // 定时器引用

// 发送验证码（不带倒计时逻辑，仅调用 API）
const sendCode = async () => {
  try {
    await userGetCodeAPI(props.email)

  } catch (err) {
    Toast(err, {
      position: 'bottom'
    })
  }

}

// 开始倒计时
const startCountdown = () => {
  isCounting.value = true
  countdown.value = 30

  if (timer) clearInterval(timer)
  timer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(timer)
      isCounting.value = false
    }
  }, 1000)
}

// 点击“获取验证码”按钮（仅在非倒计时期间可触发）
const handleGetCodeClick = () => {
  if (isCounting.value) return // 倒计时中，禁止点击
  sendCode()
  startCountdown()
}

// 页面加载时自动发送验证码并开始倒计时
onMounted(() => {
  sendCode()
  startCountdown()
})

// 组件卸载时清理定时器，防止内存泄漏
onUnmounted(() => {
  if (timer) {
    clearInterval(timer)
    timer = null
  }
})

// 表单数据
const code = ref('')
const newPassword = ref('')

// 重置密码
const resetPassword = async () => {
  const res = await userResetPasswordAPI({
    email: props.email,
    code: code.value,
    password: newPassword.value
  })
  if (res.code === 200) {
    Toast('重置密码成功', {
      position: 'bottom'
    })
    model.value = false;
  } else {
    Toast('重置密码失败请重试', {
      position: 'bottom'
    })
  }
}
</script>

<style lang="scss" scoped>
.RetrievePassword {
  @include fixed-top;
  width: 100%;
  height: 100%;
  background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#6b4bab), to(#bf09db));
  z-index: 10001;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;

  .tips {
    font-size: 12px;
    color: #999;
    margin-bottom: 10px;

    span {
      font-size: 14px;
      color: #f9cd17;
    }
  }

  .login-form {
    min-width: 330px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 20px;
    border-radius: 10px;

    h2 {
      font-size: 20px;
      font-weight: bold;
      line-height: 1;
      margin-bottom: 20px;
      text-align: center;
      color: #ccc;
    }

    .login-form-item {
      display: flex;
      gap: 5px;
      position: relative;

      .get-code {
        font-size: 12px;
        color: #fff;
        cursor: pointer;
        background-color: #dbbbe9;
        padding: 5px 10px;
        border-radius: 5px;
        font-weight: bold;
        display: flex;
        align-items: center;
        justify-content: center;
        user-select: none;

        &.disabled {
          background-color: #ccc;
          cursor: not-allowed;
          color: #999;
        }
      }

      input {
        flex: 1;
        padding: 10px;
        border-radius: 5px;
        background-color: #f9f9f9;
        color: #000;
      }
    }
  }

  .correct {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background-color: #d297fd;
    cursor: pointer;
  }
}
</style>